package com.haikesi.api.typetemplate.controller;

import com.github.pagehelper.PageInfo;
import com.haikesi.api.base.ApiIndex;
import com.haikesi.api.base.Error;
import com.haikesi.api.base.R;
import com.haikesi.api.base.Type;
import com.haikesi.api.base.annotation.Token;
import com.haikesi.api.typetemplate.domain.vo.TypeTemplateBuyVO;
import com.haikesi.api.typetemplate.domain.vo.TypeTemplateSearchVO;
import com.haikesi.api.typetemplate.domain.vo.TypeTemplateUpDeVO;
import com.haikesi.api.typetemplate.domain.vo.TypeTemplateVO;
import com.haikesi.api.typetemplate.service.ITypeTemplateService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import java.util.ArrayList;
import java.util.List;


@Api(description = "设备模板相关接口", tags = ApiIndex.TypeTemplateController)
@RequestMapping(value = "/api/typeTemplate")
@RestController
public class TypeTemplateController {

    @Autowired
    private ITypeTemplateService service;

    @ApiOperation(value = "根据设备类型ID查询模板列表")
    @GetMapping(value = "/tempListByTypeId")
    @ApiImplicitParam(name = "token", value = "签名", paramType = "query", dataType = "String")
    @Token
    public R<PageInfo<List<TypeTemplateVO>>> list(@ApiParam(value = "查询参数") @ModelAttribute TypeTemplateSearchVO typeTemplateSearchVO) {
        System.out.println("======================================================================");
        List<TypeTemplateVO> list = service.getList(typeTemplateSearchVO);
        PageInfo pageInfo = new PageInfo(list);
        return new R(pageInfo);
    }

    @ApiOperation(value = "根据设备类型码查询默认模板")
    @GetMapping(value = "/getDefaultTemp")
    @ApiImplicitParam(name = "token", value = "签名", paramType = "query", dataType = "String")
    @Token
    public R<TypeTemplateVO> getDefaultTemp(@ApiParam(value = "设备类型码") @RequestParam(value = "typeId",required = false) String typeId) {
        TypeTemplateVO defaultTemp = service.getDefaultTemp(typeId);
        return new R(defaultTemp);
    }

    @ApiOperation(value = "购买模板")
    @PostMapping(value = "")
    @ApiImplicitParam(name = "token", value = "签名", paramType = "query", dataType = "String")
    @Token
    public R create(@ApiParam(value = "添加参数") @Valid @RequestBody TypeTemplateBuyVO buyVO, BindingResult bindingResult) {
        if (bindingResult.hasErrors()) {
            List<Error> messages = new ArrayList<>();
            for (int i = 0; i < bindingResult.getFieldErrors().size(); i++) {
                messages.add(new Error(bindingResult.getFieldErrors().get(i).getField(), bindingResult.getFieldErrors().get(i).getDefaultMessage()));
            }
            return new R(Type.FORMAT_EXCEPTION).setMessages(messages);
        }
        return new R().setData(service.buyTemp(buyVO));
    }

    @ApiOperation(value = "修改默认模板")
    @PutMapping(value = "/updateDefaultTemp")
    @ApiImplicitParam(name = "token", value = "签名", paramType = "query", dataType = "String")
    @Token
    public R updateDefaultTemp(@ApiParam(value = "模板唯一标识") @Valid @RequestBody TypeTemplateUpDeVO upDeVO, BindingResult bindingResult) {
        service.updateDefaultTemp(upDeVO);
        return new R();
    }

}
